package com.example.autoboot.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

public class UsbConnectionBroadcastReceiver extends BroadcastReceiver {
    private String TAG = "UsbConnectionBroadcastReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
            // USB设备已连接
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            Log.i(TAG, "USB设备已连接: ");
            Log.i(TAG, "getDeviceName: " + device.getDeviceName());
            Log.i(TAG, "getManufacturerName: " + device.getManufacturerName());
            Log.i(TAG, "getProductName: " + device.getProductName());
            Log.i(TAG, "getProductId: " + device.getProductId());
            Log.i(TAG, "getVendorId: " + device.getVendorId());
            // 在此处处理USB设备连接的逻辑
        } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
            // USB设备已断开
            UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
            // 在此处处理USB设备断开的逻辑
            Log.i(TAG, "USB设备已断开: " + device.toString());
            Log.i(TAG, "getDeviceName: " + device.getDeviceName());
            Log.i(TAG, "getManufacturerName: " + device.getManufacturerName());
            Log.i(TAG, "getProductName: " + device.getProductName());
            Log.i(TAG, "getProductId: " + device.getProductId());
            Log.i(TAG, "getVendorId: " + device.getVendorId());
        }
    }
}
